﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace SupplyChainWin
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        private log4net.ILog log = log4net.LogManager.GetLogger("Form1");
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "开始对比数据";
            log.Info("开始对比数据");
            checkPoSug();
            label1.Text = "数据对比完成，并已处理，一分钟之后程序关闭";
            log.Info("数据对比完成，并已处理，一分钟之后程序关闭");
            Thread th = new Thread(new ThreadStart(ThreadMethod)); //也可简写为new Thread(ThreadMethod);                
            th.Start(); //启动线程  

        }
         void ThreadMethod()
        {
            Console.WriteLine("辅助线程开始...");
            Thread.Sleep(1000*60);
            //this.Close();
            System.Environment.Exit(0);
            //Console.WriteLine("辅助线程结束.");
        }

        public void checkPoSug()
        {

            string userID = System.Configuration.ConfigurationManager.AppSettings["EpiUserID"];
            string passWord = System.Configuration.ConfigurationManager.AppSettings["EpiPassword"];
            string sasUrl = System.Configuration.ConfigurationManager.AppSettings["EpiAppUrl"];

            Epicor.Mfg.Core.Session objSess;
            try
            {
                objSess = new Epicor.Mfg.Core.Session(userID, passWord, sasUrl, Epicor.Mfg.Core.Session.LicenseType.DataCollection);
                objSess.CompanyID = "001";

            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show(ex.ToString());
                objSess = null;
            }
            Epicor.Mfg.BO.POSugg posugBO;
            Epicor.Mfg.BO.POSuggDataSet dsposug;
            posugBO = new Epicor.Mfg.BO.POSugg(objSess.ConnectionPool);
            //string whereposugdtl = String.Format("VendorID = '{0}'", vendorID);
            bool morepage;
            dsposug = posugBO.GetRows("", "", 0, 0, out morepage);
            //dsposug.Copy();

            var rows = dsposug.SugPoDtl.AsEnumerable().
                                            Select(p => new
                                            {
                                                SugNum = p.Field<int>("SugNum")
                                            }).Cast<int>();
            var obj = SCMDataProvider.GetSugPoDtl2().Cast<SugPoDtl>().Where(s => !rows.Contains(s.SugNum));
            foreach (var item in obj)
            {
                SCMDataProvider.DeleteSugPoDtl(item.SugNum);
                log.InfoFormat("建议{0}已删除", item.SugNum);
            }
            //更新

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }
    }
}
